﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ICSharpCode.AvalonEdit.Highlighting;

namespace IWMAG.Scripting.DevelopmentEnvironment
{
    /// <summary>
    /// Interaction logic for BugWindow.xaml
    /// </summary>
    public partial class BugWindow : Window
    {
        public BugWindow()
        {
            InitializeComponent();
        }

        public void Setup()
        {
            mainEditor.Text = CurrentCodeText;
            cbxObservedBehaviour.SelectedIndex = (int)Status;
            switch (Status)
            {
                case CompilerStatus.CompiledSuccessfully:
                    cbxExpectedBehaviour.SelectedIndex = 1;
                    break;
                case CompilerStatus.CompileFailed:
                    cbxExpectedBehaviour.SelectedIndex = 0;
                    break;
                case CompilerStatus.CompileThrewError:
                    cbxExpectedBehaviour.SelectedIndex = 0;
                    break;
                default:
                    break;
            }
        }

        public string CurrentCodeText { get; set; }

        public CompilerStatus Status { get; set; }
        public enum CompilerStatus
        {
            CompiledSuccessfully = 0,
            CompileFailed = 1,
            CompileThrewError = 2
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            mainEditor.SyntaxHighlighting = HighlightingManager.Instance.GetDefinition("iwmag");
        }

        private void cbx_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            btnCopyAsCSharp.IsEnabled = (cbxExpectedBehaviour.SelectedIndex != cbxObservedBehaviour.SelectedIndex);
        }




        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string equality = null;
            if (cbxExpectedBehaviour.SelectedIndex == 0)
            {
                equality = "==";
            }
            else
            {
                equality = ">";
            }
           
            string toCopy2 = 
@"[TestMethod]
public void Test()
{
    string codeToTest = 
@" + "\"" +  MakeStringSuitableForCSharp(CurrentCodeText) + "\"" + @";
    var engine = new ScriptEngine(codeToTest);
    var errors = new List<IUserInfo>(engine.Compile());
    Assert.IsTrue(errors.Count " + equality + @"0" + GetCommentIfAny() +@");
}";


            Clipboard.SetText(toCopy2);
            MessageBox.Show("C# code copied.","Create a bug");
        }

        private string GetCommentIfAny()
        {
            if (tbComments.Text.Length != 0)
            {
                return ",\"" + MakeStringSuitableForCSharp(tbComments.Text) + "\"";
            }
            return string.Empty;
        }

        string MakeStringSuitableForCSharp(string s)
        {
            return new string(MakeStringSuitableForCSharpImpl(s).ToArray());
        }

        IEnumerable<char> MakeStringSuitableForCSharpImpl(string s)
        {
            for (int i = 0; i < s.Length; i++)
            {
                if (s[i] == '"')
                {
                    yield return '"';
                    yield return '+';
                    yield return '"';
                    yield return '\\';
                    yield return '"';
                    yield return '"';
                    yield return '+';
                    yield return '@';
                    yield return '"';
                    continue;
                }
                yield return s[i];
            }
        }

        string GetCurrentCodeTextSuitableForPasting()
        {
            return new string(GetCurrentCodeCharactersSuitableForPasting().ToArray());
        }

        IEnumerable<char> GetCurrentCodeCharactersSuitableForPasting()
        {
            for (int i = 0; i < CurrentCodeText.Length; i++)
            {
                if (CurrentCodeText[i] == '"')
                {
                    yield return '"';
                    yield return '+';
                    yield return '"';
                    yield return '\\';
                    yield return '"';
                    yield return '"';
                    yield return '+';
                    yield return '@';
                    yield return '"';
                    continue;
                }
                yield return CurrentCodeText[i];
            }
        }


        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
           
            Clipboard.SetText(CurrentCodeText);
            MessageBox.Show("Code Copied", "Create a bug");
        }


    }
}
